<!--岩土岩土描述列表-->
<template>
  <div class="app-container horizontal">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="岩土描述" prop="content">
          <el-input v-model="filters.content" size="small" placeholder="请输入岩土描述内容" clearable />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <superviseBtns :single="single" has-permi="geotechnicalDescription" />
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="content"
        label="岩土描述内容"
        min-width="250"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="orderNum"
        label="排序"
        min-width="40"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="createName"
        label="创建人姓名"
        min-width="80"
        align="center"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column prop="createTime" label="创建时间" align="center" min-width="150" /> -->
    </el-table>
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 岩土描述新增 -->
    <addDialog ref="addDialog" />
    <!-- 岩土描述详情-->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
// 按钮组件
import superviseBtns from '@/components/Common/Btns/superviseBtns'
import { listRocksoil, removeRocksoil } from '@/api/superviseBaseInfo/rocksoil'
export default {
  components: {
    superviseBtns,
    addDialog,
    detailDialog
  },
  data() {
    return {
      /* 搜索条件 start*/
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        content: '' // 岩土描述
      },
      dateRange: [], // 日期范围
      /* 搜索条件 end*/
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [] // 列表数据
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    // 获取列表
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listRocksoil(this.changeDateRange(this.filters, this.dateRange))
        this.tableData = rows
        this.total = total
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    /* 列表 start*/
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 列表 end*/

    /* 按钮 start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增模板')
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改模板', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('模板详情', this.multipleSelection[0].id)
    },
    // 删除
    handleDelete() {
      // 获取选中行的id
      this.$confirm('确认要删除此描述吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.loading = true
        await removeRocksoil(this.multipleSelection[0].id)
        this.loading = false
        this.msgSuccess('删除成功')
        this.getList()
      })
        .catch(() => {
          this.msgWarning('已取消删除')
        })
    }
    /* 列表 end */
  }
}
</script>

